package lee.vioson.ebook.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import lee.vioson.ebook.R
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.remotedata.bean.Book

@Composable
fun HomeBookList(
    modifier: Modifier = Modifier,
    title: String,
    list: List<Book>,
    navHostController: NavHostController,
    editable: Boolean = false,
    onDelete: ((List<Book>) -> Unit)? = null
) {
    var editMode by remember { mutableStateOf(false) }
    val selectedItem = remember { mutableStateListOf<Book>() }
    LaunchedEffect(key1 = editMode, key2 = editable, block = {
        if (!editMode) selectedItem.clear()
        if (!editable) selectedItem.clear()
    })
    Column(modifier = modifier) {
        Row(
            Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                modifier = Modifier.padding(8.dp),
                text = title, fontSize = 18.sp, color = AppTheme.colors.onBackground
            )
            Spacer(modifier = Modifier.weight(1f))
            if (editMode) {
                Text(
                    text = stringResource(id = R.string.all),
                    fontSize = 14.sp,
                    color = AppTheme.appColors.grayTextColor,
                    modifier = Modifier
                        .clickable {
                            selectedItem.addAll(list)
                        }
                        .padding(8.dp)
                )
                Text(
                    text = stringResource(id = R.string.delete),
                    fontSize = 14.sp,
                    color = AppTheme.appColors.redTextColor,
                    modifier = Modifier
                        .clickable {
                            //delete action
                            onDelete?.invoke(selectedItem.toList())
                        }
                        .padding(8.dp)
                )
            }
        }
        LazyRow(modifier = Modifier.padding(bottom = 8.dp), content = {
            item { Spacer(modifier = Modifier.width(8.dp)) }
            items(list) { item ->
                BookCard(item,
                    navHostController,
                    editable = editable,
                    initEditMode = editMode,
                    initSelected = selectedItem.contains(item),
                    onEditModeChange = { mode ->
                        editMode = mode
                    },
                    onSelectChange = { selected ->
                        if (selected) {
                            if (!selectedItem.contains(item)) {
                                selectedItem.add(item)
                            }
                        } else {
                            if (selectedItem.contains(item)) {
                                selectedItem.remove(item)
                                if (selectedItem.isEmpty()) {
                                    editMode = false
                                }
                            }
                        }
                    })
                Spacer(modifier = Modifier.width(8.dp))
            }
        })
    }
}